package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrs;
import com.atguigu.gulimall.product.vo.SkuItemVo;
import com.atguigu.gulimall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
   @Autowired
   private AttrGroupService attrGroupService;
   @Autowired
   private AttrService attrService;
   @Autowired
   private AttrAttrgroupRelationService relationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> obj.eq("attr_group_id", key).or().like("attr_group_name", key)
            );
        }
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }

    }

    @Override
    public List<AttrGroupWithAttrs> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //1.get AttrGroup by catelogId
        List<AttrGroupEntity> attrGroupEntities = attrGroupService.list(new QueryWrapper<AttrGroupEntity>()
                .eq("catelog_id", catelogId));
        //2.get attrs by attr_group-id
        List<AttrGroupWithAttrs> attrGroupWithAttrsList = attrGroupEntities.stream().map((item) -> {
            AttrGroupWithAttrs attrGroupWithAttrs = new AttrGroupWithAttrs();
            BeanUtils.copyProperties(item,attrGroupWithAttrs);
//            Long attrGroupId = item.getAttrGroupId();
//            List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = relationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>()
//                    .eq("attr_group_id", attrGroupId));
//            List<Long> attrIds = attrAttrgroupRelationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
//            List<AttrEntity> attrEntityList = attrService.listByIds(attrIds);
            List<AttrEntity> attrEntityList = attrService.getRelationAttr(item.getAttrGroupId());
            attrGroupWithAttrs.setAttrs(attrEntityList);
            return attrGroupWithAttrs;
        }).collect(Collectors.toList());
        return attrGroupWithAttrsList;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
       //1。查出当前SPU对应的所有属性的分组信息以及当前分组下的所有属性对应的值
       AttrGroupDao baseMapper=this.baseMapper;
        List<SpuItemAttrGroupVo>vos=baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
        return vos;
    }


}